<?php

Raise::load('library.oauth.request.RaiseOAuthRequest');
Raise::load('core.collection.RaiseCollection');

class RaiseOAuthRequestTokenRequest extends RaiseOAuthRequest {
    
    const KEY_CALLBACK = 'oauth_callback';
    
    /**
     * 
     * @var string
     */
    private $callback;
    
    public function __construct($header = null){
        if($header){
            $this->parse($header);
        }
    }
    
    public function parse($header){
        $a = $this->internalParse($header);
        
        $this->consumerKey = $a->get(RaiseOAuthHeader::KEY_CONSUMER_KEY);
        $this->nonce = $a->get(RaiseOAuthHeader::KEY_NONCE);
        $this->signature = $a->get(RaiseOAuthHeader::KEY_SIGNATURE);
        $this->timestamp = $a->get(RaiseOAuthHeader::KEY_TIMESTAMP);
        $this->signatureMethod = $a->get(RaiseOAuthHeader::KEY_SIGNATURE_METHOD);
        $this->version = $a->get(RaiseOAuthHeader::KEY_VERSION);
        $this->callback = $a->get(self::KEY_CALLBACK);
    }
    
    public function request() {
        $s = 'OAuth ';
        $a = new RaiseCollection();
        
        if($this->consumerKey){
            $a->add(RaiseOAuthHeader::KEY_CONSUMER_KEY . '="' . $this->consumerKey . '"');
        }
        
        if($this->nonce){
            $a->add(RaiseOAuthHeader::KEY_NONCE . '="' . $this->nonce . '"');
        }
        
        if($this->signature){
            $a->add(RaiseOAuthHeader::KEY_SIGNATURE . '="' . $this->signature . '"');
        }
        
        if($this->signatureMethod){
            $a->add(RaiseOAuthHeader::KEY_SIGNATURE_METHOD . '="' . $this->signatureMethod . '"');
        }
        
        if($this->timestamp){
            $a->add(RaiseOAuthHeader::KEY_TIMESTAMP . '="' . $this->timestamp . '"');
        }
        
        if($this->version){
            $a->add(RaiseOAuthHeader::KEY_VERSION . '="' . $this->version . '"');
        }
        
        if($this->callback){
            $a->add(self::KEY_CALLBACK . '="' . $this->callback . '"');
        }
        
        $s .= $a->join(', ');
        return $s;
    }

    /**
     * Get or set $description$
     * @param string $c (optional) $description$
     * @return string
     */
    public function callback($c = null){
        if(func_num_args() == 1){
            $this->callback = $c;
        }
        return $this->callback;
    }
}
